package org.mspring.mlog.scheduler.job;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.mspring.mlog.ServiceFactory;
import org.mspring.mlog.entity.Attachment;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Post;
import org.mspring.platform.qiniu.QiniuStorageService;
import org.mspring.platform.security.entity.User;
import org.mspring.platform.utils.http.HttpUtils;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiniu.api.io.PutRet;

/**
 * 采集器正在重构，这个只是为了暂时补充一下博客文章
 *
 * @author Gao Youbo
 * @since 2014年5月16日
 */
public class ITQiushiSpiderJob extends BaseJob {

    private static final Logger log = LoggerFactory.getLogger(ITQiushiSpiderJob.class);

    @Override
    protected void nativeExecuteInternal(JobExecutionContext context) {
        int maxPage = 94;
        for (int i = 1; i <= maxPage; i++) {
            try {
                List<String> urls = getPostUrls(i);
                for (String url : urls) {
                    getPost(url);
                }
            } catch (Exception e) {
                log.error(null, e);
            }
        }
    }

    public List<String> getPostUrls(int page) throws IOException {
        List<String> result = new ArrayList<>();
        String pageUrl = "http://www.itqushi.com/page/" + page;
        Document doc = Jsoup.connect(pageUrl).timeout(8000).get();
        Elements elements = doc.select(".post h1.title > a");
        for (Element element : elements) {
            result.add(element.absUrl("href"));
        }
        return result;
    }

    public Post getPost(String postUrl) throws Exception {
        log.info("采集文章：" + postUrl);
        Post post = new Post();
        post.setAuthor(new User(new Long(1)));
        post.setCreateTime(new Date());
        post.setModifyTime(new Date());
        post.setIsTop(false);
        post.setReplys(new Long(0));
        post.setViews(new Long(0));
        post.setStatus(Post.Status.PUBLISH);
        post.setCommentStatus(Post.CommentStatus.OPEN);
        Set<Catalog> catalogs = new HashSet<>();
        catalogs.add(new Catalog(new Long(36)));
        post.setCatalogs(catalogs);

        Document doc = Jsoup.connect(postUrl).timeout(8000).get();
        String title = doc.select("h1.article-title").get(0).text();
        post.setTitle(title);

        Element content = doc.select("#main #apost").get(0);
        List<Attachment> attachs = handleImage(content);
        post.setContent(content.html());
        post = ServiceFactory.getPostService().create(post);
        if (attachs != null && !attachs.isEmpty()) {
            for (Attachment a : attachs) {
                a.setForeignKey(post.getId());
                ServiceFactory.getAttachmentService().create(a);
            }
        }
        return post;
    }

    public List<Attachment> handleImage(Element content) throws Exception {
        if (content == null) {
            return null;
        }
        Elements images = content.select("img");
        if (images == null || images.isEmpty()) {
            return null;
        }

        List<Attachment> attachs = new ArrayList<>();
        QiniuStorageService qiniuStorageService = ServiceFactory.getQiniuStorageService();
        for (Element image : images) {
            String originUrl = image.absUrl("src");
            log.info("抓取图片：" + originUrl);
            HttpResponse httpResponse = HttpUtils.get(originUrl);
            long size = httpResponse.getEntity().getContentLength();
            PutRet ret = qiniuStorageService.put(httpResponse.getEntity().getContent());
            String url = qiniuStorageService.getUrl(ret);
            if (StringUtils.isNotBlank(url)) {
                Attachment attach = new Attachment();
                attach.setCreateTime(new Date());
                attach.setForeignType(Attachment.AttachFrom.FROM_POST);
                attach.setUrl(url);
                attach.setUser(new Long(1));
                attach.setSize(size);
                attachs.add(attach);
                content.attr("src", url);
            }
        }
        return attachs;
    }
}